﻿namespace EasyWeb.Web.UI
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web;

    public abstract class EWViewComponentBuilder<TComponent, TBuilder>
        where TComponent : EWViewComponent
        where TBuilder : EWViewComponentBuilder<TComponent, TBuilder>
    {
        protected EWViewComponentBuilder(TComponent component)
        {
            EWGuard.IsNotNull(component, "component");

            this.Component = component;
        }

        public TComponent Component { get; private set; }

        public virtual TBuilder Name(string componentName)
        {
            this.Component.Name = componentName;
            return (TBuilder)this;
        }

        public virtual TBuilder HtmlAttributes(object attributes)
        {
            return this.HtmlAttributes(attributes.ToDictionary());
        }

        public virtual TBuilder HtmlAttributes(IDictionary<string, object> attributes)
        {
            EWGuard.IsNotNull(attributes, "attributes");

            this.Component.HtmlAttributes.Clear();
            this.Component.HtmlAttributes.Merge(attributes);

            return (TBuilder)this;
        }

        public virtual void Render()
        {
            this.Component.Render();
        }

        public IHtmlString ToHtmlString()
        {
            return this.Component.ToHtmlString();
        }

        public override string ToString()
        {
            return this.Component.ToString();
        }
    }
}
